import { createApp, ref } from 'vue'
import './style.css'
import App from './App.vue'

const app = createApp(App);

// ViewUIPlus组件库
import ViewUIPlus from 'view-ui-plus'
import 'view-ui-plus/dist/styles/viewuiplus.css'
import '@/assets/css/myTheme.less';
import 'element-plus/dist/index.css'
// 国际化
import i18n from '@/langs/index'

app.use(i18n);
app.use(ViewUIPlus, {
	i18n
});

//引入vxe-table
import VXETable from "vxe-table";
import "vxe-table/lib/style.css";

function useTable(app) {
	app.use(VXETable);
}
app.use(useTable);

// pinia 数据持久化
import { createPinia } from 'pinia'
import piniaPluginPersist from 'pinia-plugin-persist'
// echarts图表
import * as echarts from 'echarts'
const store = createPinia();
store.use(piniaPluginPersist);
app.use(store);

import { userStore } from '@/store/userInfo';
import { navTabStore } from '@/store/navTab';

// 路由
import router from './router'
app.use(router);
router.afterEach(() => {
	window.scrollTo(0, 0)
});

const userInfo = userStore();
const navTab = navTabStore();
router.beforeEach((to, from, next) => {
	if (to.path != '/login' && !userInfo.token) {
		next('/login');
	} else {
		if (to.path != '/login') {
			navTab.addVisitedRoute(to);
		}
		next();
	}
});

// 请求
import { myGet, myPost, formPost, downloadfile } from './util/request'
app.config.globalProperties.$get = myGet;
app.config.globalProperties.$post = myPost;
app.config.globalProperties.$downloadfile = downloadfile;
app.config.globalProperties.$echarts = echarts;
app.config.globalProperties.$formPost = formPost;

// 弹窗
import CommonDialog from '@/components/CommonDialog/index.vue'
app.component('CommonDialog', CommonDialog);
// 表格
import TemplateTable from '@/components/TemplateTable/index.vue'
app.component('TemplateTable', TemplateTable);
// 表单
import OptionForm from '@/components/OptionForm/index.vue'
app.component('OptionForm', OptionForm);

// 搜索组件
import SearchGroup from '@/components/SearchGroup.vue'
app.component('SearchGroup', SearchGroup);

// svg图标组件
import svgIcon from '@/components/svgIcon/index.vue';
import "virtual:svg-icons-register";
app.component('svg-icon', svgIcon);

// 表格按钮组件
import textBtn from '@/components/textButton.vue'
app.component('text-btn', textBtn);

// 初始化时获取屏幕高度
let screenHeight = ref(window.innerHeight);

// 防抖函数
function debounce(fn, delay) {
	let timer;
	return function (...args) {
		clearTimeout(timer);
		timer = setTimeout(() => fn.apply(this, args), delay);
	};
}
// 更新高度的函数
const updateHeight = () => {
	screenHeight.value = window.innerHeight;
};

// 使用防抖包装更新函数
const debouncedUpdateHeight = debounce(updateHeight, 300);

window.addEventListener('resize', debouncedUpdateHeight);

// 提供全局高度
app.provide('screenHeight', screenHeight);

app.mount('#app');